package website.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import website.interceptor.LoginInterceptor;
import website.interceptor.RefreshTokenInterceptor;

/**
 * Web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 注册登录拦截
     * @return
     */
    @Bean
    public LoginInterceptor getLoginInterceptor() {
        return new LoginInterceptor();
    }

    /**
     * 注册刷新token拦截
     * @return
     */
    public RefreshTokenInterceptor getRefreshTokenInterceptor(){
        return new RefreshTokenInterceptor(redisTemplate);
    }

    /**
     * 添加登录拦截
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getLoginInterceptor())
                .addPathPatterns(
                        "/api/user",
                        "/api/article",
                        "/api/comment",
                        "/api/comment/reply",
                        "/api/link",
                        "/api/photo",
                        "/api/resource"
                )
                .order(1);
                //.excludePathPatterns("/api/user/code","/api/user/avatar","/api/user/login","/api/user/register","/api/user/forget");

        // token刷新的拦截器
        registry.addInterceptor(getRefreshTokenInterceptor())
                .addPathPatterns("/**")
                .order(0);
    }
}
